package io.renren.modules.zhaopin.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import io.renren.common.esign.EsignHttpResponse;
import io.renren.common.utils.*;
import io.renren.modules.zhaopin.dao.BaseAuthInfoDao;
import io.renren.modules.zhaopin.dto.CandIdateInfo;
import io.renren.modules.zhaopin.dto.ReturnNotifyDto;
import io.renren.modules.zhaopin.entity.*;
import io.renren.modules.zhaopin.service.*;
import io.renren.modules.zhaopin.util.HTTPHelper;
import io.renren.modules.zhaopin.util.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Slf4j
@Service("baseAuthInfoService")
public class BaseAuthInfoServiceImpl extends ServiceImpl<BaseAuthInfoDao, BaseAuthInfoEntity> implements BaseAuthInfoService {

    @Value("${esign.host}")
    private String host;
    @Value("${esign.appId}")
    private String appId;
    @Value("${esign.appSecret}")
    private String appSecret;
    @Value("${esign.redirectUrl}")
    private String redirectUrl;
    @Value("${esign.notifyUrl}")
    private String notifyUrl;
    @Value("${background.tokenUrl}")
    private String tokenUrl;
    @Value("${background.appId}")
    private String backgroundAppId;
    @Value("${background.appSecret}")
    private String backgroundAppSecret;
    @Value("${background.grantType}")
    private String grantType;

    @Autowired
    private BackgroundCheckOrdersService backgroundCheckOrdersService;
    @Autowired
    private BaseItemResourceService baseItemResourceService;
    @Autowired
    private BaseWorkExperienceService baseWorkExperienceService;
    @Autowired
    private CertifierService certifierService;
    @Autowired
    private BackgroundCustomInfoService backgroundCustomInfoService;

    @Autowired
    private BaseAuthInfoService baseAuthInfoService;
    @Autowired
    private IdWorker idWorker;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BaseAuthInfoEntity> page = this.page(
                new Query<BaseAuthInfoEntity>().getPage(params),
                new QueryWrapper<BaseAuthInfoEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 生成word文件
     *
     * @param reportId 报告id
     */
    private void generateWordFile(String reportId) {
        LambdaQueryWrapper<BackgroundCheckOrdersEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BackgroundCheckOrdersEntity::getReportId, reportId);
        BackgroundCheckOrdersEntity checkOrders = backgroundCheckOrdersService.getOne(queryWrapper);
        try {
            File file = ResourceUtils.getFile("classpath:授权声明.docx");
            Map<String, Object> params = new HashMap<>();
            // 渲染文本
            params.put("company", checkOrders.getOrgName());
            params.put("id_card", checkOrders.getIdCard());
            params.put("date", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));

            String path = file.getPath();
            String fileDir = path.substring(0, path.lastIndexOf("授"));

            String templatePath = file.getPath(); // "D:\\zdd.docx";
            String fileName = "最终版授权声明";

            String wordPath = GeneratorWordUtils.createWord(templatePath, fileDir, fileName, params);
            System.out.println("生成文档路径：" + wordPath);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 上传本地文件
     *
     * @return
     */
    private Map<String, Object> uploadMFile() {
        String contentType = "application/pdf";

        String postUrl = "/v3/files/file-upload-url";
        String postAllUrl = host + postUrl;
        try {
            File file = ResourceUtils.getFile("classpath:最终版授权声明.docx");
            String path = file.getPath();
            String contentMD5 = ESignUtils.getFileContentMD5(path);
            // 计算签名拼接的url
            // 构建请求Body体
            JSONObject reqBodyObj = new JSONObject();
            reqBodyObj.put("contentMd5", contentMD5);
            reqBodyObj.put("contentType", contentType);
            reqBodyObj.put("convertToPDF", true);
            reqBodyObj.put("fileName", "最终版授权声明.docx");
            reqBodyObj.put("fileSize", file.length() + "");

            Map<String, Object> resultMap = getStringObjectMapPost(reqBodyObj, postUrl, postAllUrl);
            Map<String, Object> dataMap = (Map<String, Object>) resultMap.get("data");
            String fileUploadUrl = (String) dataMap.get("fileUploadUrl");

            // 文件上传
            Gson gson = new Gson();
            EsignHttpResponse uploadFileResponse = ESignUtils.uploadFile(fileUploadUrl, path, contentMD5);
            JSONObject uploadFileResponseJsonObject = gson.fromJson(uploadFileResponse.getBody(), JSONObject.class);
            String code = uploadFileResponseJsonObject.get("errCode").toString();
            System.out.println("文件上传成功，状态码:" + code);

            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
            System.out.println(msg);
            throw new RuntimeException(e);
        }
    }


    private Map<String, Object> getStringObjectMapPost(JSONObject reqBodyObj, String postUrl, String postAllUrl) throws Exception {
        // 请求Body体数据
        String reqBodyData = reqBodyObj.toString();
        // 对请求Body体内的数据计算ContentMD5
        String contentMD5 = ESignUtils.getBodyContentMD5(reqBodyData);
        System.out.println("请求body数据:" + reqBodyData);
        System.out.println("body的md5值：" + contentMD5);

        // 构建待签名字符串
        String method = "POST";
        String accept = "*/*";
        String contentType = "application/json; charset=UTF-8";
        String date = "";
        String headers = "";

        StringBuffer sb = new StringBuffer();
        sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
                .append(contentType).append("\n").append(date).append("\n");
        if ("".equals(headers)) {
            sb.append(headers).append(postUrl);
        } else {
            sb.append(headers).append("\n").append(postUrl);
        }

        // 构建参与请求签名计算的明文
        String plaintext = sb.toString();
        // 计算请求签名值
        String reqSignature = ESignUtils.doSignatureBase64(plaintext, appSecret);
        System.out.println("计算请求签名值:" + reqSignature);

        // 获取时间戳(精确到毫秒)
        long timeStamp = DateUtils.timeStamp();

        // 构建请求头
        LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
        header.put("X-Tsign-Open-App-Id", appId);
        header.put("X-Tsign-Open-Auth-Mode", "Signature");
        header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
        header.put("Accept", accept);
        header.put("Content-Type", contentType);
        header.put("X-Tsign-Open-Ca-Signature", reqSignature);
        header.put("Content-MD5", contentMD5);
        System.out.println("header" + header);

        String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
        JSONObject resultObj = JSONObject.parseObject(result);
        // json转map
        Map<String, Object> resultMap = (Map<String, Object>) JSONObject.toJavaObject(resultObj, Map.class);
        System.out.println("请求返回信息： " + resultObj.toString());
        System.out.println("请求返回信息： " + resultMap.toString());
        return resultMap;
    }

    /**
     * 创建签署流程发送请求API
     *
     * @param reqBodyObj 请求体
     * @param postUrl    url
     * @param postAllUrl 全url
     * @return 返回数据
     * @throws Exception 异常处理
     */
    private Map<String, Object> getCreatePostSign(JSONObject reqBodyObj, String postUrl, String postAllUrl) throws Exception {
        // 请求Body体数据
        String reqBodyData = reqBodyObj.toString();
        // 对请求Body体内的数据计算ContentMD5
        String contentMD5 = ESignUtils.getBodyContentMD5(reqBodyData);
        System.out.println("请求body数据:" + reqBodyData);
        System.out.println("body的md5值：" + contentMD5);

        // 构建待签名字符串
        String method = "POST";
        String accept = "*/*";
        String contentType = "application/json;charset=UTF-8";
        String date = "";
        String headers = "";

        StringBuffer sb = new StringBuffer();
        sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
                .append(contentType).append("\n").append(date).append("\n");
        if ("".equals(headers)) {
            sb.append(headers).append(postUrl);
        } else {
            sb.append(headers).append("\n").append(postUrl);
        }

        // 构建参与请求签名计算的明文
        String plaintext = sb.toString();
        // 计算请求签名值
        String reqSignature = ESignUtils.doSignatureBase64(plaintext, appSecret);
        System.out.println("计算请求签名值:" + reqSignature);

        // 获取时间戳(精确到毫秒)
        long timeStamp = DateUtils.timeStamp();

        // 构建请求头
        LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
        header.put("X-Tsign-Open-App-Id", appId);
        header.put("X-Tsign-Open-Auth-Mode", "Signature");
        header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
        header.put("Accept", accept);
        header.put("Content-Type", contentType);
        header.put("X-Tsign-Open-Ca-Signature", reqSignature);
        header.put("Content-MD5", contentMD5);
        System.out.println("header" + header);

        String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
        JSONObject resultObj = JSONObject.parseObject(result);
        // json转map
        Map<String, Object> resultMap = (Map<String, Object>) JSONObject.toJavaObject(resultObj, Map.class);
        int code = (int) resultMap.get("code");
        if (code == 0) {
            Map<String, Object> dataMap = (Map<String, Object>) resultMap.get("data");
            // 签署流程id
            String signFlowId = (String) dataMap.get("signFlowId");
            resultMap.put("signFlowId", signFlowId);
        }
        System.out.println("请求返回信息： " + resultObj.toString());
        return resultMap;
    }

    /**
     * 签署流程id 获取签署页面链接
     *
     * @param signFlowId 签署流程id
     */
    private String getSignUrl(String signFlowId) {
        String postAllUrl = host + "/v3/sign-flow/" + signFlowId + "/sign-url";
        String postUrl = "/v3/sign-flow/" + signFlowId + "/sign-url";

        Map<String, Object> operatorMap = new HashMap<>();
        operatorMap.put("psnAccount", "16619880853");
        JSONObject reqBodyObj = new JSONObject();
        reqBodyObj.put("clientType", "ALL");
        reqBodyObj.put("needLogin", false);
        reqBodyObj.put("urlType", 2);
        reqBodyObj.put("operator", operatorMap);
        Map<String, Object> redirectConfigMap = new HashMap<>();
        // 签署完成后跳转页面（需符合 https /http 协议地址）
        redirectConfigMap.put("redirectUrl", redirectUrl);
        reqBodyObj.put("redirectConfig", redirectConfigMap);
        System.out.println("reqBodyObj" + reqBodyObj);
        try {
            Map<String, Object> map = getStringObjectMapPost(reqBodyObj, postUrl, postAllUrl);
            System.out.println(map);
            int code = (int) map.get("code");
            if (code == 0) {
                Map<String, Object> dataMap = (Map<String, Object>) map.get("data");
                String shortUrl = (String) dataMap.get("shortUrl");
                return shortUrl;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 查询文件上传状态
     *
     * @param fileId 报告id
     * @return
     */
    private Integer getFileStatus(String fileId) throws Exception {
        String getAllUrl = host + "/v3/files/" + fileId;
        String postUrl = "/v3/files/" + fileId;
        JSONObject reqBodyObj = new JSONObject();
        reqBodyObj.put("fileId", fileId);

        // GET请求时ContentMD5为""
        String contentMD5 = "";

        // 构建待签名字符串
        String method = "GET";
        String accept = "*/*";
        String contentType = "application/json; charset=UTF-8";
        String date = "";
        String headers = "";

        StringBuffer sb = new StringBuffer();
        sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
                .append(contentType).append("\n").append(date).append("\n");
        if ("".equals(headers)) {
            sb.append(headers).append(postUrl);
        } else {
            sb.append(headers).append("\n").append(postUrl);
        }

        // 构建参与请求签名计算的明文
        String plaintext = sb.toString();
        // 计算请求签名值
        String reqSignature = ESignUtils.doSignatureBase64(plaintext, appSecret);
        System.out.println("计算请求签名值：" + reqSignature);
        // 获取时间戳(精确到毫秒)
        long timeStamp = DateUtils.timeStamp();
        // 构建请求头
        LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
        header.put("X-Tsign-Open-App-Id", appId);
        header.put("X-Tsign-Open-Auth-Mode", "Signature");
        header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
        header.put("Accept", accept);
        header.put("Content-Type", contentType);
        header.put("X-Tsign-Open-Ca-Signature", reqSignature);
        header.put("Content-MD5", contentMD5);

        HashMap<String, Object> query = new HashMap<String, Object>();
        // 发送POST请求
        String result = HTTPHelper.sendGet(getAllUrl, query, header, "UTF-8");
        JSONObject resultObj = JSONObject.parseObject(result);
        System.out.println("请求返回信息： " + resultObj.toString());
        Map<String, Object> resultMap = (Map<String, Object>) JSONObject.toJavaObject(resultObj, Map.class);
        System.out.println(resultMap);
        return (Integer) resultMap.get("code");
    }

    /**
     * 获取文件坐标
     */
    private Map<String, Object> getPosition(String fileId) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        String postUrl = "/v3/files/" + fileId + "/keyword-positions";
        String postAllUrl = host + postUrl;

        JSONObject reqBodyObj = new JSONObject();
        reqBodyObj.put("fileId", fileId);
        List<String> keywords = new ArrayList<>();
        keywords.add("本人签名");
        reqBodyObj.put("keywords", keywords);
        try {
            Map<String, Object> map = getStringObjectMapPost(reqBodyObj, postUrl, postAllUrl);
            System.out.println(map);
            Map<String, Object> dataMap = (Map<String, Object>) map.get("data");
            if (dataMap == null) {
                throw new RuntimeException("解析失败");
            }
            List keywordPositions = (List) dataMap.get("keywordPositions");
            Map<String, Object> objectMap = (Map<String, Object>) keywordPositions.get(0);
            List positions = (List) objectMap.get("positions");
            Map<String, Object> positionMap = (Map<String, Object>) positions.get(0);
            List coordinates = (List) positionMap.get("coordinates");
            Integer pageNums = (Integer) positionMap.get("pageNum");
            Map<String, Object> position = (Map<String, Object>) coordinates.get(0);
            position.put("pageNums", pageNums);
            return position;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 签署
     *
     * @param reportId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R sign(String reportId) {
        try {
            String id = idWorker.nextId() + "";
            // 1.生成文件
            generateWordFile(reportId);
            // 2.上传文件
            Map<String, Object> resultMap = uploadMFile();
            Map<String, Object> dataMap = (Map<String, Object>) resultMap.get("data");
            String fileId = (String) dataMap.get("fileId");
            String fileUploadUrl = (String) dataMap.get("fileUploadUrl");
            System.out.println(resultMap);
            /*
              1. 查看所上传文件的当前状态（转换pdf/html文件状态）文件名称和下载链接。
              2. 当返回的文件状态status值为 2 或 5 时，此文件才可以被应用到签署流程中。
             */
            Integer fileStatus = getFileStatus(fileId);
            if (fileStatus == 0) {
                Map<String, Object> position = getPosition(fileId);
                BigDecimal positionY = (BigDecimal) position.get("positionY");
                BigDecimal positionX = (BigDecimal) position.get("positionX");
                Integer pageNums = (Integer) position.get("pageNums");
                // 3.创建签署流程
                Map<String, Object> sign = createSign(fileId, reportId, positionX, positionY, pageNums);
                String signUrl = (String) sign.get("signUrl");
                String signFlowId = (String) sign.get("signFlowId");
                // 保存到数据库 fileUploadId
                BaseAuthInfoEntity baseAuthInfoEntity = new BaseAuthInfoEntity();
                baseAuthInfoEntity.setId(id);
                baseAuthInfoEntity.setOrderId(reportId);
                baseAuthInfoEntity.setAuthFlowId(signFlowId);
                baseAuthInfoEntity.setAuthUrl(signUrl);
                baseAuthInfoEntity.setCreateTime(new Date());
                baseAuthInfoEntity.setUpdateTime(new Date());
                baseAuthInfoEntity.setFileId(fileId);
                baseAuthInfoEntity.setFileUploadUrl(fileUploadUrl);
                baseAuthInfoService.save(baseAuthInfoEntity);
                return R.ok(sign);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return R.ok("签署流程失败");
    }

    /**
     * 签署回调通知
     *
     * @param returnNotifyDto
     */
    @Override
    public Map<String, String> returnNotify(ReturnNotifyDto returnNotifyDto) {
        boolean update = false;
        String action = returnNotifyDto.getAction();
        if ("SIGN_MISSON_COMPLETE".equalsIgnoreCase(action)) {
            // 签署流程id
            String signFlowId = returnNotifyDto.getSignFlowId();
            // 签署描述
            String resultDescription = returnNotifyDto.getResultDescription();
            // signResult 签署结果 2 - 签署完成，4 - 拒签
            String signResult = returnNotifyDto.getSignResult();
            BaseAuthInfoEntity baseAuthInfoEntity = new BaseAuthInfoEntity();
            baseAuthInfoEntity.setAuthFlowId(signFlowId);
            baseAuthInfoEntity.setAuthStatus(signResult);
            baseAuthInfoEntity.setAuthExplain(resultDescription);
            LambdaUpdateWrapper<BaseAuthInfoEntity> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(BaseAuthInfoEntity::getAuthFlowId, signFlowId);
            update = this.update(baseAuthInfoEntity, updateWrapper);

            LambdaQueryWrapper<BaseAuthInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BaseAuthInfoEntity::getAuthFlowId, signFlowId);
            BaseAuthInfoEntity info = this.getOne(queryWrapper);
            String authUrl = info.getAuthUrl();
            String reportId = info.getOrderId();
            order(reportId, authUrl);
        }

        Map<String, String> result = new HashMap<>();
        if (update) {
            result.put("code", "200");
            result.put("msg", "success");
        } else {
            result.put("code", "201");
            result.put("msg", "success");
        }
        return result;
    }

    /**
     * 获取云背调接口token
     */
    @Override
    public Map<String, Object> getToken() {
        String postAllUrl = tokenUrl + "/api/backgroundcheck/access/token" + "?AppID=" + backgroundAppId + "&AppSecret=" + backgroundAppSecret + "&GrantType=" + grantType;
        HashMap<String, Object> map = new HashMap<String, Object>();
        LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
        try {
            String s = HTTPHelper.sendGet(postAllUrl, map, header, "UTF-8");
            // 字符串转json
            JSONObject jsonObject = JSONObject.parseObject(s);
            Integer code = (Integer) jsonObject.get("code");
            JSONObject data = (JSONObject) jsonObject.get("data");
            String token = (String) data.get("token");

            Map<String, Object> result = new HashMap<>();
            result.put("code", code);
            result.put("token", token);
            System.out.println(s);
            return result;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 下单接口同步
     */
    public String order(String reportId, String signUrl) {
        String url = tokenUrl + "/api/backgroundcheck/access/token" + "?AppID=" + backgroundAppId + "&AppSecret=" + backgroundAppSecret + "&GrantType=" + grantType;
        HashMap<String, Object> map = new HashMap<String, Object>();
        LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
        JSONObject jsonObjects = null;
        try {
            String s = HTTPHelper.sendGet(url, map, header, "UTF-8");
            // 字符串转json
            jsonObjects = JSONObject.parseObject(s);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        Integer code = (Integer) jsonObjects.get("code");
        if (code == 200) {
            JSONObject data = (JSONObject) jsonObjects.get("data");
            String token = (String) data.get("token");

            LambdaQueryWrapper<BaseItemResourceEntity> itemResourceEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            itemResourceEntityLambdaQueryWrapper.eq(BaseItemResourceEntity::getOrderId, reportId);
            BaseItemResourceEntity baseItemResourceEntity = baseItemResourceService.getOne(itemResourceEntityLambdaQueryWrapper);
            if (baseItemResourceEntity == null) {
                return "未找到报告Id";
            }
            String baseInfo = baseItemResourceEntity.getBaseInfo();
            // 字符串转json
            JSONObject jsonObject = JSONObject.parseObject(baseInfo);

            LambdaQueryWrapper<BaseWorkExperienceEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BaseWorkExperienceEntity::getOrderId, reportId);
            List<BaseWorkExperienceEntity> list = baseWorkExperienceService.list(queryWrapper);
            list.forEach(baseWorkExperienceEntity -> {
                String id = baseWorkExperienceEntity.getId();
                LambdaQueryWrapper<CertifierEntity> certifierEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
                certifierEntityLambdaQueryWrapper.eq(CertifierEntity::getWorkExperienceId, id);
                List<CertifierEntity> certifierEntities = certifierService.list(certifierEntityLambdaQueryWrapper);
                baseWorkExperienceEntity.setWitnessList(certifierEntities);
            });

            LambdaQueryWrapper<BackgroundCheckOrdersEntity> backgroundCheckOrdersEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            backgroundCheckOrdersEntityLambdaQueryWrapper.eq(BackgroundCheckOrdersEntity::getReportId, reportId);
            BackgroundCheckOrdersEntity backgroundCheckOrdersEntity = backgroundCheckOrdersService.getOne(backgroundCheckOrdersEntityLambdaQueryWrapper);
            String customItemId = backgroundCheckOrdersEntity.getCustomItemId();
            LambdaQueryWrapper<BackgroundCustomInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(BackgroundCustomInfoEntity::getCustomItemId, customItemId);
            BackgroundCustomInfoEntity customInfo = backgroundCustomInfoService.getOne(lambdaQueryWrapper);

            CandIdateInfo candidateInfo = new CandIdateInfo();
            candidateInfo.setName(backgroundCheckOrdersEntity.getHrName());
            candidateInfo.setReportId(reportId);
            candidateInfo.setIdcard(backgroundCheckOrdersEntity.getIdCard());
            candidateInfo.setPhone(backgroundCheckOrdersEntity.getHrTel());
            candidateInfo.setCompany(backgroundCheckOrdersEntity.getOrgName());
            candidateInfo.setPosition("");
            candidateInfo.setItemList(customInfo.getChooseList());
            candidateInfo.setRemark(backgroundCheckOrdersEntity.getRemark());
            candidateInfo.setAuthShortUrl(signUrl);

            String postAllUrl = tokenUrl + "/api/backgroundcheck/create/order";
            HashMap<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("candidateInfo", candidateInfo);
            resultMap.put("baseInfo", jsonObject);
            resultMap.put("workExperienceList", list);
            LinkedHashMap<String, String> headers = new LinkedHashMap<String, String>();
            headers.put("AppId", backgroundAppId);
            headers.put("token", token);
            headers.put("Content-Type", "application/json");
            // map转json
            String jsonString = JSON.toJSONString(resultMap);
            try {
                String s = HTTPHelper.sendPOST(postAllUrl, jsonString, headers, "UTF-8");
                System.out.println(s);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return "成功";
    }


    /**
     * 创建签署流程
     */
    public Map<String, Object> createSign(String fileId, String reportId, BigDecimal positionX, BigDecimal positionY, Integer pageNums) {
        LambdaQueryWrapper<BackgroundCheckOrdersEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BackgroundCheckOrdersEntity::getReportId, reportId);
        BackgroundCheckOrdersEntity checkOrders = backgroundCheckOrdersService.getOne(queryWrapper);

        Map<String, Object> paramsMap = new HashMap<>();

        Map<String, Object> docsMap = new HashMap<>();
        // 待签署文件ID
        docsMap.put("fileName", "最终版授权声明.docx");
        docsMap.put("fileId", fileId);

        Map<String, Object> signFlowConfigMap = new HashMap<>();
        signFlowConfigMap.put("signFlowTitle", "企业合同签署");
        // 接收相关回调通知的Web地址
        signFlowConfigMap.put("notifyUrl", notifyUrl);
        Map<String, Object> redirectConfigMap = new HashMap<>();
        redirectConfigMap.put("redirectUrl", redirectUrl);
        signFlowConfigMap.put("redirectConfig", redirectConfigMap);
        signFlowConfigMap.put("autoFinish", true);

        Map<String, Object> signersMap = new HashMap<>();
        Map<String, Object> signConfigMap = new HashMap<>();
        signConfigMap.put("forcedReadingTime", 5);
        /*
            签署方类型，0 - 个人，1 - 企业/机构，2 - 法定代表人，3 - 经办人
            若指定签署方为个人，则psnSignerInfo为必传项；
            若指定签署方为机构或法定代表人手动签署（autoSign参数为false）时，则orgSignerInfo为必传项；
            若指定签署方为经办人，在同级数组内必须还有机构类型存在，且orgSignerInfo为必传项，即：指定3 - 经办人签的前提是必须同时存在1 - 企业/机构，且经办人签属于企业合同，不在个人名下。
         */
        signersMap.put("signerType", 0);
        signersMap.put("signConfig", signConfigMap);

        Map<String, Object> psnSignerInfoMap = new HashMap<>();
        // 企业/机构名称（账号标识）
        psnSignerInfoMap.put("psnAccount", checkOrders.getHrTel());
        signersMap.put("psnSignerInfo", psnSignerInfoMap);

        List<Object> docsList = new ArrayList<>();
        docsList.add(docsMap);
        paramsMap.put("docs", docsList);
        paramsMap.put("signFlowConfig", signFlowConfigMap);
        List<Object> signersList = new ArrayList<>();
        signersList.add(signersMap);

        List<Object> signFieldsList = new ArrayList<>();

        Map<String, Object> signFieldsMap = new HashMap<>();
        signFieldsMap.put("fileId", fileId);
        signFieldsMap.put("customBizNum", reportId);

        Map<String, Object> normalSignFieldConfigMap = new HashMap<>();
        // 1 - 单页签章，2 - 骑缝签章
        normalSignFieldConfigMap.put("signFieldStyle", 1);
        normalSignFieldConfigMap.put("freeMode", false);
        normalSignFieldConfigMap.put("autoSign", false);
        Map<String, Object> signFieldPositionMap = new HashMap<>();
        signFieldPositionMap.put("positionX", positionX);
        signFieldPositionMap.put("positionY", positionY);
        signFieldPositionMap.put("positionPage", pageNums);

        signFieldsMap.put("normalSignFieldConfig", normalSignFieldConfigMap);
        normalSignFieldConfigMap.put("signFieldPosition", signFieldPositionMap);

        signFieldsList.add(signFieldsMap);
        signersMap.put("signFields", signFieldsList);

        String postAllUrl = host + "/v3/sign-flow/create-by-file";
        String postUrl = "/v3/sign-flow/create-by-file";
        JSONObject reqBodyObj = new JSONObject();
        reqBodyObj.put("docs", docsList);
        reqBodyObj.put("signFlowConfig", signFlowConfigMap);
        reqBodyObj.put("signers", signersList);
        System.out.println("reqBodyObj:" + reqBodyObj);
        System.out.println("paramsMap:" + paramsMap);
        try {
            Map<String, Object> map = getCreatePostSign(reqBodyObj, postUrl, postAllUrl);
            String signFlowId = (String) map.get("signFlowId");
            // 签署流程id 获取签署页面链接
            String signUrl = getSignUrl(signFlowId);
            map.put("signUrl", signUrl);
            map.put("signFlowId", signFlowId);
            return map;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}